*TROOP DEPLOYMENT AND PUBLIC OPINION
*FLYNN, MARTINEZ MACHAIN, AND STOYAN
*FILE CREATED BY ALISSANDRA STOYAN ON 2017.01.30
*LAST UPDATED BY ALISSANDRA STOYAN ON 2017.02.06
*LAST UPDATED BY MICHAEL FLYNN ON 2017.02.08
*LAST UPDATED BY ALISSANDRA STOYAN ON 2017.02.09
*LAST UPDATED BY MICHAEL FLYNN ON 2017.02.12
*LAST UPDATED BY ALISSANDRA STOYAN ON 2017.02.15
*LAST UPDATED BY MICHAEL FLYNN ON 2017.02.16
*LAST UPDATED BY ALISSANDRA STOYAN ON 2017.02.27
*LAST UPDATED BY ALISSANDRA STOYAN ON 2017.02.28
*LAST UPDATED BY ALISSANDRA STOYAN ON 2017.03.16
*LAST UPDATED BY MICHAEL FLYNN ON 2017.03.17

********************************

*use "C:\Users\stoyan\Dropbox\Troops and Public Opinion in Latin America\Data\Raw Data\LAPOP\Peru\1257201564Peru LAPOP AmericasBarometer 2014 v3.0_W.dta", clear
*cd "C:\Users\stoyan\Dropbox\Troops and Public Opinion in Latin America\Tables"
use "1257201564Peru LAPOP AmericasBarometer 2014 v3.0_W.dta", clear
cd ""

*use "C:\Users\carlamm\Dropbox\Working Papers\Troops and Public Opinion in Latin America\Data\Raw Data\LAPOP\Peru\1257201564Peru LAPOP AmericasBarometer 2014 v3.0_W.dta" , clear
*cd "C:\Users\carlamm\Dropbox\Working Papers\Troops and Public Opinion in Latin America\Tables"

********************************
*IDENTIFYING LAPOP VARIABLES OF INTEREST AND CONFIRMING OBSERVATIONS
********************************

*log using "keyvariables", text replace
set more off

*Opinions of the U.S. in general
*Most influence in Lat Am? (4) = U.S.
tab for1n
*In 10 years who will have most influence? (4) = U.S.
tab for4
*Model of development for our future? (4) = U.S.
tab for5
*How much influence does the U.S. have in our country?
tab for6b
*U.S. influence is very positive/positive/negative/very negative?
tab for7b
*The U.S. government is very/somewhat/a little/not at all trustworthy?
tab mil10e

*Political Knowledge of the U.S.
*What is the name of the current U.S. President?
tab gi1

*Opinions of the Peruvian Military
*How much confidence do you have in the Armed Forces?
tab b12
*To what extent do you think that the Peruvian Armed Forces respect Peruvian citizens' human rights today?
tab b3milx

*Opinions of the U.S. Military
*To what extent do you trust the U.S. Armed Forces?
tab mil3
*To what extent should the U.S. Armed Forces work with the Peruvian Armed Forces to improve national security?
tab mil4

*Satisfaction with President Humala
tab m1

*Satisfaction with infrastructure, schools, and public health
tab sd2new2
tab sd3new2
tab sd6new2

*National economic situation
tab soct2
*Personal economic situation
tab idio2

*log close


********************************
*GENERATING KEY CONTROL VARIABLES
********************************

*SOCIOECONOMIC STATUS
/*Scale measuring household well-being based on
ownership of *14* consumer durables. Scale is constructed
by weighting each item according to its prevalence in the
country, with more prevalent items carrying less weight.
Higher values indicate higher SES.*/
*Weight calculated by 100-%withGood)

gen r5o = .
replace r5o=0 if r5==0
replace r5o=1 if r5==1 | r5==2 | r5==3

set more off
tab r1
*ANY TYPE OF TV - 6.55
tab r3
*REFRIGERATOR - 35.00
tab r4
*TELEPHONE - 61.24
tab r4a
*CELL PHONE - 16.28
tab r5o
*VEHICLE - 85.86
tab r6
*WASHING MACHINE - 65.33
tab r7
*MICROWAVE - 69.07
tab r8
*MOTORCYCLE - 82.97
tab r12
*POTABLE WATER - 10.27
tab r14
*BATHROOM - 14.61
tab r15
*COMPUTER - 56.22
tab r16
*FLAT SCREEN TV - 62.67
tab r18
*INTERNET - 65.26
tab r26
*CONNECTED TO SEWER SYSTEM - 13.28

gen ses=.
replace ses = ((r1*6.55) + (r3*35.00) + (r4*61.24) + (r4a*16.28) + (r5o*85.86) + (r6*65.33) + (r7*69.07) + (r8*82.97) + (r12*10.27) + (r14*14.61) + (r15*56.22) + (r16*62.67) + (r18*65.26) + (r26*13.28))/25.7844

alpha r1 r3 r4 r4a r5o r6 r7 r8 r12 r14 r15 r16 r26, asis
*scale reliability coefficient = 0.7859

*AGE
gen q2sq=q2*q2

*LANGUAGE
gen lang=.
replace lang=0 if leng1==1101
replace lang=1 if leng1>=1102
label define labellang 0 "Spanish" 1 "Other"
label values lang labellang

*EDUCATION
gen edsc=ed
recode edsc 1/5=1 6/8=2 9/11=3 12=4 13/15=5 16=6 17/18=7
label define labeleduc 0 "None" 1 "Primary, 1st-5th Grade" 2 "Primary, 6th-8th Grade" 3 "Incomplete Secondary" 4 "Complete Secondary" 5 "Incomplete University" 6 "Complete University" 7 "Graduate School"
label values edsc labeleduc

*REMITTANCES
gen remit=2-q10a
label define labelremit 0 "no" 1 "yes"
label values remit labelremit

*URBAN/RURAL
gen urban1=2-ur
label define labelurban1 0 "rural" 1 "urbano"
label values urban1 labelurban1

gen urban2=5-tamano
label define labelurban2 0 "Area rural" 1 "Ciudad pequena" 2 "Ciudad mediana" 3 "Ciudad grande" 4 "Capital Nacional (area metropolitana)"
label values urban2 labelurban2

*IDEOLOGY (LEFT=0, RIGHT=10)
*fine as coded

*PRESIDENTIAL APPROVAL
gen presapproval=5-m1
label define labelpresapproval 0 "Muy malo (pesimo)" 1 "Malo" 2 "Ni bueno ni malo" 3 "Bueno" 4 "Muy bueno"
label values presapproval labelpresapproval

*OPINION ABOUT CHINA
gen chgovtrust=.
replace chgovtrust=4-mil10a
label define labels3 0 "not at all trustworthy" 1 "a little trustworthy" 2 "somewhat trustworthy" 3 "very trustworthy"
label values chgovtrust labels3

*CHINESE INFLUENCE - AMOUNT AND CHARACTER
gen for6_new = 4-for6
label define labels1 0 "none" 1 "a little" 2 "some" 3 "a lot"
label values for6_new labels1

gen for7_new=.
replace for7_new=-2 if for7==5
replace for7_new=-1 if for7==4
replace for7_new=0 if for7==3
replace for7_new=1 if for7==2
replace for7_new=2 if for7==1

gen chinfl_peru = .
replace chinfl_peru = for6_new*for7_new if for6_new!=. & for7_new!=.
label define labels2 -6 "a lot/v. neg" -4 "some/v. neg" -3 "a lot/neg." -2 "a little/v. neg or some/neg" -1 "a little/neg" 0 "none/neutral" 1 "a little/pos" 2 "a little/v. pos or some/pos" 3 "a lot/pos" 4 "some/v. pos" 6 "a lot/v. pos"
label values chinfl_peru labels2

*OPINION ABOUT IRAN
gen irgovtrust=.
replace irgovtrust=4-mil10c
*label define labels3 0 "not at all trustworthy" 1 "a little trustworthy" 2 "somewhat trustworthy" 3 "very trustworthy"
label values irgovtrust labels3

*SATISFACTION MEASURES
gen satinfra=.
replace satinfra=4-sd2new2
label define labelssat 0 "Very Unsatisfied" 1 "Unsatisfied" 2 "Satisfied" 3 "Very Satisfied"
label values satinfra labelssat

gen satedu=.
replace satedu=4-sd3new2
label values satedu labelssat

gen satmed=.
replace satmed=4-sd6new2
label values satmed labelssat

*TRUST IN GOVERNMENT INSTITUTIONS
*ADDITIVE TRUST_I
gen trust_i=b1+b11+b13+b17+b21a
alpha b1 b11 b13 b17 b21a

*POLYCHORIC FACTOR ANALYSIS
*mean-centering variables
gen b1_c = b1 - 3.261138
gen b11_c = b11 - 3.813179 
gen b13_c = b13 - 2.644159
gen b17_c = b17 - 3.807273 
gen b21a_c = b21a - 3.329293

polychoric b1_c b11_c b13_c b17_c b21a_c
display r(sum_w)
global N = r(sum_w)
matrix r = r(R)
factormat r, n($N) factors(3)
rotate, promax
predict trust_f

*SOCIOTROPIC VIEWS OF THE ECONOMY
gen sociotrop=.
replace sociotrop=-1 if soct2==3
replace sociotrop=0 if soct2==2
replace sociotrop=1 if soct2==1
label define labelssociotrop -1 "Worse" 0 "Same" 1 "Better"
label values sociotrop labelssociotrop

*INTERPERSONAL TRUST
gen iptrust=4-it1
label define labelsiptrust 0 "Not at all trustworthy" 1 "A little trustworthy" 2 "Somewhat trustworthy" 3 "Very trustworthy"
label values iptrust labelsiptrust

*AZPURU 2016
*POLITICAL INTEREST - FREQUENCY OF FOLLOWING THE NEWS
gen polint=5-gi0
label define labelspolint 0 "Never" 1 "Rarely" 2 "Several times a month" 3 "Several times a week" 4 "Daily"
label values polint labelspolint

*AZPURU & BONIFACE 2015
*PERCEPTIONS OF INSECURITY
tab aoj11

*NATIONALISM (PRIDE) - orgullo por el sistema politico
tab b4


********************************
*GENERATING TROOP DEPLOYMENT GEOGRAPHIC VARIABLES
********************************

*DEPARTMENT/REGION DUMMY (1 if U.S. Troops)
gen ustd_d=0
replace ustd_d=1 if prov==1113 | prov==1114 | prov==1115 | prov==1120 | prov==1111 | prov==1109 | prov==1105
replace ustd_d=1 if perprov==1501

*SINCE 2012 DEPARTMENT/REGION DUMMY (1 if U.S. Troops in last 2 years)
gen ustd_d2=0
replace ustd_d2=1 if prov==1111 | prov==1109
replace ustd_d2=1 if perprov==1501

*PROVINCE DUMMY (1 if U.S. Troops)
gen ustd_p=0
replace ustd_p=1 if perprov==1201 | perprov==1508 | perprov==1102 | perprov==901 | perprov==906 | perprov==1101 | perprov==1501 | perprov==501 | perprov==1403 

*SINCE 2012 PROVINCE DUMMY (1 if U.S. Troops in last 2 years)
gen ustd_p2=0
replace ustd_p2=1 if perprov==1102 | perprov==901 | perprov==906 | perprov==1101 | perprov==1501

*DEPARTMENT/REGION COUNT
gen ustd_dc=0
replace ustd_dc=1 if prov==1113 | prov==1113 | prov==1120 | prov==1105
replace ustd_dc=2 if perprov==1501 | prov==1109
replace ustd_dc=3 if prov==1111





********************************
*RECODING POTENTIAL VARS OF INTEREST
********************************

*US INFLUENCE
gen usinfl_la=0
replace usinfl_la=1 if for1n==4

gen usinfl_la_f=0
replace usinfl_la_f=1 if for4==4

corr usinfl_la usinfl_la_f
corr usinfl_la_f ustd_d

*US MODEL OF DEVELOPMENT
gen usmoddev=0
replace usmoddev=1 if for5==4

corr usmoddev ustd_p2

corr ustd_p for6b
corr ustd_d for7b

*US INFLUENCE - AMOUNT AND CHARACTER
gen for6b_new = 4-for6b
*label define labels1 0 "none" 1 "a little" 2 "some" 3 "a lot"
label values for6b_new labels1

gen for7b_new=.
replace for7b_new=-2 if for7b==5
replace for7b_new=-1 if for7b==4
replace for7b_new=0 if for7b==3
replace for7b_new=1 if for7b==2
replace for7b_new=2 if for7b==1

gen usinfl_peru = .
replace usinfl_peru = for6b_new*for7b_new if for6b_new!=. & for7b_new!=.
*label define labels2 -6 "a lot/v. neg" -4 "some/v. neg" -3 "a lot/neg." -2 "a little/v. neg or some/neg" -1 "a little/neg" 0 "none/neutral" 1 "a little/pos" 2 "a little/v. pos or some/pos" 3 "a lot/pos" 4 "some/v. pos" 6 "a lot/v. pos"
label values usinfl_peru labels2

corr ustd_d usinfl_peru
corr ustd_p usinfl_peru

*The U.S. government is very/somewhat/a little/not at all trustworthy?
tab mil10e

gen usgovtrust=.
replace usgovtrust=4-mil10e
*label define labels3 0 "not at all trustworthy" 1 "a little trustworthy" 2 "somewhat trustworthy" 3 "very trustworthy"
label values usgovtrust labels3

keep prov mil3 usgovtrust usinfl_peru 

collapse (mean) mil3 usgovtrust usinfl_peru, by(prov)

format mil3 usgovtrust usinfl_peru %9.1fc

decode prov, gen(region) 
replace region = "Huánuco" if prov == 1110
replace region = "Junín" if prov == 1112
replace region = "San Martín" if prov == 1122
replace region = "Apurímac" if prov == 1103

drop prov

tempfile opiniondata
save `opiniondata', replace


clear
shp2dta using "PER_adm1.shp", data(Peru_adm_data) coordinates(Peru_adm_coord) genid(ID) replace
use "Peru_adm_data.dta"
keep ISO NAME_1 ID
rename NAME_1 region
sort region
tempfile clear
tempfile map_temp
save `map_temp', replace

merge 1:1 region  using `opiniondata'   

	spmap mil3 using Peru_adm_coord, id(ID)  fcolor(Blues) ///
	ndfcolor(none) mosize(*.15) ndocolor(black) ndsize(*.15 ..) ocolor(black) osize(*.15 ..) ///
	legtitle("Opinion of US Military") legend(on) clnumber(3) gsize(5) name(mil3map, replace) 

	spmap usgovtrust using Peru_adm_coord, id(ID)  fcolor(Blues) ///
	ndfcolor(none) mosize(*.15) ndocolor(black) ndsize(*.15 ..) ocolor(black) osize(*.15 ..) ///
	legtitle("Opinion of US Government") legend(on) clnumber(3) gsize(5) name(usgovtrustmap, replace)

	spmap usinfl_peru using Peru_adm_coord, id(ID)  fcolor(Blues) ///
	ndfcolor(none) mosize(*.15) ndocolor(black) ndsize(*.15 ..) ocolor(black) osize(*.15 ..) ///
	legtitle("Opinion of US Influence") legend(on) clmethod(custom) clbreaks(-3 -2 -1 0 1 2 3) gsize(5) name(usinfl_perumap, replace)

	do "Figures_Map_Peru.do"

	graph combine mapgraphcolor mil3map usgovtrustmap usinfl_perumap, cols(2) xsize(9) ysize(12) imargin(0 0 0 0) scale(.9)

	graph export "Combined Map.pdf", replace
